/**
* ...
* @author Default
* @version 0.1
*/

package bdz.projects.slab.core {
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.display.MovieClip;
	import flash.system.LoaderContext;
	import flash.text.*;
	import flash.utils.getDefinitionByName;
	import as3.common.utils.BitmapUtils;
	import as3.common.utils.TextAnimator;
	import as3.common.utils.TweenLite;
	import fl.transitions.easing.*;
	import flash.geom.*;
	import flash.system.ApplicationDomain;
	public class mainLoader extends Sprite {
		private var myloader:Loader;
		private var preloaderField:TextField;
		private var bg:MovieClip;
		private var bar:MovieClip;
		private var bwidth:Number = 1000;
		private var ease = Strong.easeOut;
		private var container:MovieClip;
		private var holder:MovieClip;
		function mainLoader(){
			super();			
			addEventListener(Event.ADDED_TO_STAGE, initialize);
		}
		private function cleanMeUp(evt:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE, initializeComponent);
			removeEventListener(Event.ENTER_FRAME, centerField);
		}
		private function ptextFormat():TextFormat{
			var format:TextFormat = new TextFormat();
			var fontObj:Object = getDefinitionByName("pixelate");
			format.color = 0xFFFFFF;
			format.font = new fontObj().fontName;
			format.size = 10;
			return format;
		}
		private function setUpField(field:TextField):TextField{
			field.autoSize = "left";
			field.antiAliasType = AntiAliasType.NORMAL;
			field.gridFitType = GridFitType.PIXEL;
			field.embedFonts = true;
			field.selectable = false;
			field.multiline = true;
			return field;
		}
		private function centerField(evt:Event):void{
			try {
				bg.width = stage.stageWidth;
				bg.height = stage.stageHeight;
				preloaderField.y = Math.ceil(stage.stageHeight/2-preloaderField.height/2);
				bar.y = preloaderField.y+preloaderField.height+5;
				
				
			}catch(e:Error){
				
			}
		}
		private function addLoadedContent():void{
			TweenLite.to(bg, 2, {ease:ease, alpha:0}, 0, removeBar, null, false);
			container.addChild(Sprite(myloader.content));
			myloader.unload();
		
			removeChild(bar);		
			
		}
		private function removeBar():void{
			removeChild(bar);
			removeChild(bg);
		}
		private function initialize(evt:Event):void {
			stage.align = "TL";
			stage.scaleMode = "noScale";
			
			holder = new MovieClip();
			addChild(holder);
			
			container = new MovieClip();
			addChild(container);
			bg = new defStage();
			addChild(bg);
			bar = new MovieClip();
			BitmapUtils.createBounds(bar, bwidth, 1, 1, 0xFFFFFF);
			addChild(bar);
			try{
				myloader = new Loader();
				myloader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
				myloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
				myloader.load(new URLRequest("main.swf"));
			}catch(e:Error){
				
			}
			preloaderField = new TextField();
			preloaderField.setTextFormat(ptextFormat());
			preloaderField.defaultTextFormat = ptextFormat();
			setUpField(preloaderField);
			addChild(preloaderField);
			addEventListener(Event.ENTER_FRAME, centerField);
			
		}
		private function loadProgress(evt:ProgressEvent):void{
			var tot:Number = int((evt.bytesLoaded/evt.bytesTotal)*100)
			TweenLite.to(bar, .9, {scaleX:tot/100, ease:ease}, 0, null, null, false);
			preloaderField.text = "Loading SLab (Silverlens Lab) Website\n";
			preloaderField.text += tot.toString() + "% complete";
		}
		private function removePreloaderField():void{
			preloaderField.text = "";
			preloaderField.width = 0;
			preloaderField.height = 0;
			holder.removeChild(preloaderField);
		}
		private function moveBar():void{
			TweenLite.to(bar, .9, {ease:ease, alpha:0}, 0, addLoadedContent, null, false);
		}
		private function loadComplete(evt:Event):void{
			
			removeEventListener(Event.ENTER_FRAME, centerField);
			TweenLite.to(bar, .9, {ease:ease, width:1000}, 0, moveBar, null, false);
			TweenLite.to(preloaderField, .9, {alpha:0}, 0, removePreloaderField);
			
		}
	}
	
}